//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.lyyzoo.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public abstract class ObjectMappers {
    private static final String DateFormat = "yyyy-MM-dd HH:mm:ss";
    private static final boolean PrettyFormat = false;

    public ObjectMappers() {
    }

    private static ObjectMapper newMapper(String dateFormat, ObjectMapper mapper) {
        if (mapper == null) {
            mapper = new ObjectMapper();
            mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, true);
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
            mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
            if (dateFormat == null) {
                dateFormat = "yyyy-MM-dd HH:mm:ss";
            }

            mapper.setDateFormat(Dates.newDateFormat(dateFormat));
        }

        return mapper;
    }

    public static ObjectMapper newMapper() {
        return newMapper((String)null, (ObjectMapper)null);
    }

    public static ObjectMapper newMapper(String dateFormat) {
        return newMapper(dateFormat, (ObjectMapper)null);
    }

    public static ObjectMapper newMapper(ObjectMapper mapper) {
        return newMapper((String)null, mapper);
    }

    public static String toJSONString(Object o) {
        return toJSONString(o, (String)null, (ObjectMapper)null, false);
    }

    public static String toJSONString(Object o, boolean prettyFormat) {
        return toJSONString(o, (String)null, (ObjectMapper)null, prettyFormat);
    }

    public static String toJSONString(Object o, String dateFormat) {
        return toJSONString(o, dateFormat, (ObjectMapper)null, false);
    }

    public static String toJSONString(Object o, String dateFormat, boolean prettyFormat) {
        return toJSONString(o, dateFormat, (ObjectMapper)null, prettyFormat);
    }

    public static String toJSONString(Object o, ObjectMapper mapper) {
        return toJSONString(o, (String)null, mapper, false);
    }

    public static String toJSONString(Object o, ObjectMapper mapper, boolean prettyFormat) {
        return toJSONString(o, (String)null, mapper, prettyFormat);
    }

    private static String toJSONString(Object o, String dateFormat, ObjectMapper mapper, boolean prettyFormat) {
        String jsonString = null;

        try {
            mapper = newMapper(mapper);
            if (dateFormat != null) {
                mapper.setDateFormat(Dates.newDateFormat(dateFormat));
            }

            if (prettyFormat) {
                jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(o);
            } else {
                jsonString = mapper.writeValueAsString(o);
            }

            return jsonString;
        } catch (JsonProcessingException var6) {
            throw Exceptions.unchecked(var6);
        }
    }

    public static <T> T toBean(String jsonString, Class<T> clazz) {
        return toBean(jsonString, clazz, (TypeReference)null, (ObjectMapper)null);
    }

    public static <T> T toBean(String jsonString, Class<T> clazz, ObjectMapper mapper) {
        return toBean(jsonString, clazz, (TypeReference)null, mapper);
    }

    public static <T> T toBean(String jsonString, TypeReference<?> typeReference) {
        return toBean(jsonString, null, typeReference, (ObjectMapper)null);
    }

    public static <T> T toBean(String jsonString, TypeReference<?> typeReference, ObjectMapper mapper) {
        return toBean(jsonString, null, typeReference, mapper);
    }

    private static <T> T toBean(String jsonString, Class<T> clazz, TypeReference<?> type, ObjectMapper mapper) {
        try {
            mapper = newMapper(mapper);
            return type != null ? (T)mapper.readValue(jsonString, type) : (T)mapper.readValue(jsonString, clazz);
        } catch (Exception var5) {
            throw Exceptions.unchecked(var5);
        }
    }

    public static <T> List<T> toList(String jsonString, Class<T> clazz) {
        return toList(jsonString, clazz, (ObjectMapper)null);
    }

    public static <T> List<T> toList(String jsonString, Class<T> clazz, ObjectMapper mapper) {
        List<T> objs = new ArrayList();
        List<Map<String, Object>> maps = (List)toBean(jsonString, List.class, mapper);
        if (maps != null) {
            Iterator var5 = maps.iterator();

            while(var5.hasNext()) {
                Map<String, Object> map = (Map)var5.next();
                objs.add(convert(map, clazz, mapper));
            }
        }

        return objs;
    }

    public static Map<String, Object> toMap(Object obj) {
        return (Map)newMapper().convertValue(obj, Map.class);
    }

    public static Map<String, Object> toMap(String jsonString) {
        return (Map)toBean(jsonString, Map.class);
    }

    public static Map<String, Object> toMap(String jsonString, ObjectMapper mapper) {
        return (Map)toBean(jsonString, Map.class, mapper);
    }

    public static <T> T convert(Object fromValue, Class<T> toValueType) {
        return convert(fromValue, toValueType, (ObjectMapper)null);
    }

    public static <T> T convert(Object fromValue, Class<T> toValueType, ObjectMapper mapper) {
        return newMapper(mapper).convertValue(fromValue, toValueType);
    }
}
